package com.zengqingfa.demo.aop;

/*
 *  @ author  zengqingfa
 *  @ created in    2020/3/29  20:23
 *
 */

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;


@Aspect
@Component
public class UserServiceAspect {

    //拦截指定的方法
    @Pointcut("execution(* com.zengqingfa.demo.service.impl.UserServiceImpl.getById(..))")
    private void getByIdPointCut() {
    }


    @Around("getByIdPointCut()")
    public Object getByIdAround(ProceedingJoinPoint pjp) throws Throwable {
        //获取参数
        Object[] args = pjp.getArgs();
        System.out.println("参数args:" + args[0]);
        //执行方法前
        Object returnVal = null;
        //执行方法
        try {
            Long start = System.currentTimeMillis();
            returnVal = pjp.proceed();
            //执行方法后
            System.out.println("执行了：" + (System.currentTimeMillis() - start) + "s");
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return returnVal;
    }

}
